package com.phx.serialjni;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.utils.PakageMod;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class gActivity extends Activity {

    private GridView gridview;
    private PackageManager pManager;
    private List<PakageMod> datas;
    private List<PakageMod> imgs;
    private String tag = "gActivity";
    private String[] datax;
    private int[] imgids;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_maing);
        // 取得gridview
        gridview = (GridView) findViewById(R.id.gridview1);
        // 获取图片、应用名、包名
        pManager = gActivity.this.getPackageManager();
        List<ResolveInfo> appList = getAllApps(gActivity.this);
        datas = new ArrayList<PakageMod>();
        int sizex = appList.size();
        datax = new String[sizex];
        imgids = new int[sizex];
        for (int i = 0; i < appList.size(); i++) {
            ResolveInfo pinfo = appList.get(i);
            PakageMod shareItem = new PakageMod();
            // 设置图片
//            包名获取方法：resolve.activityInfo.packageName
//            icon获取获取方法：resolve.loadIcon(packageManager)
//            应用名称获取方法：resolve.loadLabel(packageManager).toString()

            //  PackageManager packageManager = PackageManager.get(pinfo.activityInfo.packageName;
//             shareItem.icon = pinfo.loadIcon(pinfo.activityInfo.packageName);
//           // String ppp = pinfo.activityInfo.packageName;
            //  shareItem.icon =pinfo.activityInfo.icon;
//           // Resources resources=pm.getResourcesForApplication(PackageName);
//            // 设置应用程序名字
            // 设置图片
            shareItem.icon = pManager.getApplicationIcon(pinfo.activityInfo.applicationInfo);
            shareItem.appName = pManager.getApplicationLabel(
                    pinfo.activityInfo.applicationInfo).toString();
            // shareItem.appName = pinfo.activityInfo.name;
//            // 设置应用程序的包名
            shareItem.pakageName = pinfo.activityInfo.packageName;

            // Resources resources=pinfo..getResourcesForApplication(shareItem.pakageName);
            //(shareItem.pakageName);
            //  Resources resources = pinfo.applicationInfo.get
            // int indentify = resources.getIdentifier(pinfo.applicationInfo.icon,null,null);


            datas.add(shareItem);
            datax[i]= shareItem.appName;
            imgids[i] = pinfo.icon;


        }

        gridview.setAdapter(new DemoGridAdapter(this,datas));

        // 点击应用图标时，做出响应
        gridview.setOnItemClickListener(new ClickListener());
        hideBottomUIMenu();
    }


    protected void hideBottomUIMenu() {
        //隐藏虚拟按键，并且全屏
        if (Build.VERSION.SDK_INT > 11 && Build.VERSION.SDK_INT < 19) { // lower api
            View v = this.getWindow().getDecorView();
            v.setSystemUiVisibility(View.GONE);
        } else if (Build.VERSION.SDK_INT >= 19) {
            //for new api versions.
            View decorView = getWindow().getDecorView();
            int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
            decorView.setSystemUiVisibility(uiOptions);
        }
    }
    @Override
    protected void onDestroy() {
        // uartJni.close(fd);
//        if(uartJnit != null) {
//            uartJnit.sclose();
//        }
        super.onDestroy();
    }

//    static List<ApkInfo> GetApks(PackageManager packageManager) {
//        Intent intent = new Intent("android.intent.action.MAIN", null);
//        intent.addCategory("android.intent.category.LAUNCHER");
//        List<ResolveInfo> queryIntentActivities = packageManager.queryIntentActivities(intent, 0);
//        List<ApkInfo> arrayList = new ArrayList();
//        for (ResolveInfo resolveInfo : queryIntentActivities) {
//            Object obj;
//            Iterator it = omitApks.iterator();
//            while (it.hasNext()) {
//                if (((String) it.next()).compareToIgnoreCase(resolveInfo.activityInfo.packageName) == 0) {
//                    obj = 1;
//                    break;
//                }
//            }
//            obj = null;
//            if (obj == null) {
//                arrayList.add(new ApkInfo(resolveInfo.loadIcon(packageManager), (String) resolveInfo.loadLabel(packageManager), resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name));
//            }
//        }
//        return arrayList;
//    }

    public static List<ResolveInfo> getAllApps(Context context) {

        List<ResolveInfo> apps = new ArrayList<ResolveInfo>();
        PackageManager pManager = context.getPackageManager();
        // 获取手机内所有应用
        Intent intent = new Intent("android.intent.action.MAIN", null);
        intent.addCategory("android.intent.category.LAUNCHER");
        //   List<ResolveInfo> queryIntentActivities = pManager.queryIntentActivities(intent, 0);
        List<ResolveInfo> packlist = pManager.queryIntentActivities(intent, 0);
        List<PackageInfo> ppacklist = pManager.getInstalledPackages(0);

        for (int i = 0; i < packlist.size(); i++) {
            ResolveInfo pak = (ResolveInfo) packlist.get(i);
            // PackageInfo pak = (PackageInfo) packlist.get(i);
            // if((pak.applicationInfo.flags& pak.applicationInfo.FLAG_SYSTEM) != 0)
            //  if((pak.activityInfo.packageName.compareToIgnoreCase()
            // {
            apps.add(pak);
            //  }
            // if()里的值如果<=0则为自己装的程序，否则为系统工程自带
//            if ((pak.applicationInfo.flags & pak.applicationInfo.FLAG_SYSTEM) <= 0) {
//                // 添加自己已经安装的应用程序
//                 apps.add(pak);
//            }
            // apps.add(pak);
        }
        return apps;
    }

    private class ClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                long arg3) {
            Intent intent = new Intent();
            intent = gActivity.this.getPackageManager()
                    .getLaunchIntentForPackage(datas.get(position).pakageName);
            startActivity(intent);
        }
    }
}